//
//  YBEntertainingLampLabelView.m
//  ShangHaiHuDongApp
//
//  Created by apple on 17/9/25.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "YBEntertainingLampLabelView.h"

@interface YBEntertainingLampLabelView ()
{
    //字体大小
    CGFloat _font;
    //view大小
    CGRect _frame;
}
@end

@implementation YBEntertainingLampLabelView

- (instancetype)initWithFrame:(CGRect)frame font:(CGFloat)font textColor:(UIColor *)textColor
{
    self = [super initWithFrame:frame];
    if (self) {
        _font = font;
        _frame = frame;
        self.label = [ToolClass creatLabelWithFrame:frame font:font textColor:textColor text:@""];
        [self addSubview:self.label];
        self.clipsToBounds = YES;
    }
    return self;
}

-(void)setLabelString:(NSString *)labelString
{
    [self.label.layer removeAllAnimations];
    _labelString = labelString;
    CGFloat titleWidth = [labelString boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : LYPFONT(_font)} context:nil].size.width;
    self.label.text = labelString;
    if (titleWidth + 1 >= _frame.size.width) {
        self.label.frame = CGRectMake(_frame.size.width, 0, titleWidth + _frame.size.width , _frame.size.height);
        self.label.layer.anchorPoint = CGPointMake(0, 0.5);
        CABasicAnimation * basicAnimation = [CABasicAnimation animation];
        basicAnimation.keyPath = @"position.x";
        basicAnimation.fromValue = @(_frame.size.width);
        basicAnimation.toValue = @(-titleWidth);
        basicAnimation.duration = titleWidth / (_frame.size.width) * 8;
        basicAnimation.repeatCount = INT_MAX;
        basicAnimation.beginTime = CACurrentMediaTime();
        [self.label.layer addAnimation:basicAnimation forKey:@"basicAnimation"];
    }
}

@end
